﻿using System;
using ZX.Common;
using ZX.DBModule;
using ZX.Web.UI.Controls;
using ZX.BLL.Extend;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Linq;
using ZX.Common.Extend;

namespace ZX.WebApp
{
    public partial class AutoDTEditUI : EditUIPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CreateUI();
        }

        private void CreateUI()
        {
            string strEntity = this.GetStringFromQueryString("entity", "E_Menu");
            string action = this.GetStringFromQueryString("action", "");

            string id = this.GetStringFromQueryString("id", "");
           
            DataTableBaseEntity instance = new DataTableBaseEntity();
            instance.TableName = strEntity;
            string[] strArrary = instance.GetKey();
            if (strArrary != null)
            {
                instance.TableKey = strArrary[0];
                instance.KeyType = strArrary[1];
            }
            instance.CreateDT();
            instance.EntityJson = ZX.Common.Globals.DataRowToJSON(instance.DRow);
            //BaseEntity instance = ZX.Common.Globals.getEntity(strEntity) as BaseEntity;
            BaseBussnies bInstance = new BLL.Extend.BaseBussnies(instance);
            DataTableBaseEntity entity = null;
            if (id != "") //有ID表示编辑
            {
                entity = new DataTableBaseEntity(); //bInstance.GetEntityById(id) as BaseEntity;
                entity.TableName = strEntity;
                entity.TableKey = instance.TableKey;
                entity.KeyType = instance.KeyType;
                entity.CreateDT(id);
                entity.EntityJson = ZX.Common.Globals.DataRowToJSON(entity.DRow);
            }
            this.EditTemplate.Text = CreateHtml(instance, entity);
            EditUIConfig ConfigEntity = EditUIConfig.New();
            bInstance.entity = ConfigEntity;
            bInstance.Order = "iOrder";
            string formReForm = this.GetStringFromQueryString("formReForm", "false");

            IList<EditUIConfig> list = bInstance.GetEntitys<EditUIConfig>("EntityName='" + strEntity + "'");
            int? ColIndex = list.Max(p => p.ColumnIndex);
            StringBuilder strHtml = new StringBuilder();
            if (!string.Equals(formReForm, "false"))
            {
                strHtml.Append(CreateEditReForm(list));
            }
            else
            {
                strHtml.Append("<Table id=\"EditForm\"><tr>");
                for (int i = 0; i < ColIndex + 1; i++)
                {
                    strHtml.Append("<td valign='top' id=\"Col" + i.ToString() + "\"> ");
                    if (action.ToLower() == "view")
                    {
                        strHtml.Append(CreateViewForm(entity, list.Where(p => p.ColumnIndex == i).ToList()));
                    }
                    else
                    {
                        if (entity == null)
                        {
                            strHtml.Append(CreateEditForm(list.Where(p => p.ColumnIndex == i).ToList()));
                        }
                        else
                        {
                            strHtml.Append(CreateEditForm(entity, list.Where(p => p.ColumnIndex == i).ToList()));
                        }
                    }
                    strHtml.Append("</td>");
                }
                strHtml.Append("</tr></table>");
            }
            FormTemplate.Text = strHtml.ToString();
            this.jSave.Text = CreateScript(list);
            this.jInitControl.Text = InitControl(list);
            this.MasterID.Text = SetMasterID();
        }

        /// <summary>
        /// 行为参数
        /// </summary>
        public String Action
        {
            get
            {
                return this.GetStringFromQueryString("action", "");
            }

        }
    }

}